<?php

// This class is used to work out whether the user is able to save resources in particular locations based on content they contain.

class permissions {

	private $m_userObj;
	
	private $m_childAssets = array("charts" => array(), "images" => array(), "imagesets" => array());
	private $m_restrictedAssets = array("charts" => array(), "images" => array(), "imagesets" => array());
	
	private $m_saveLocation;
	private $m_saveType;
	
	private $m_resourceType;
	
	private $m_assetID;
	private $m_targetID;
	
	public $m_hasGroupImages;
	public $m_hasPrivateImages;
	
	// setResource() $type
	const CHART 	= 1;
	const IMAGESET 	= 2;
	const IMAGE 	= 3;
	const BOOK 		= 4;
	
	// setResource() $saveType
	const RESOURCE_EXISTING = 1;
	const RESOURCE_NEW 		= 2;

	// setLocation() $location
	const LOCATION_PRIVATE 	= 1;
	const LOCATION_GROUP 	= 2;
	const LOCATION_PUBLIC 	= 3;
	const LOCATION_CURRENT	= 4;

	public function __construct(user &$userObj){
		$this->m_userObj = $userObj;
	}
	
	public function setResource($type, $saveType = permissions::RESOURCE_NEW, $id = 0){
		if(is_integer($type) == false || is_integer($saveType) == false || is_integer($id) == false) throw new PicBoardException("All arguments to this function must be integers.");
		$this->m_resourceType = $type;
		$this->m_saveType = $saveType;
		$this->m_assetID = $id;
	}

	public function setLocation($location = permissions::LOCATION_CURRENT, $id = 0){
		if(is_integer($location) == false) throw new PicBoardException("All arguments to this function must be integers.");
		$this->m_saveLocation = $location;
		$this->m_targetID = $id;
	}
		
	
	
	// Can we publish this resource? returns a boolean.
	public function canPublish(){
	
		switch($this->m_resourceType){
		
			case permissions::CHART:
			
			break;
			
			case permissions::IMAGESET:
			
			break;
			
			case permissions::IMAGE:
			
			break;
			
			case permissions::BOOK:
				// We need to check what all the child assets contain. We cannot publish if they contain anything other than mulberry.
				$this->getChildAssets();
			break;
		
		
		}
	
	}
	
	public function canDonate(){}
	
	public function canRetrieve(){}
	
	public function canDelete(){}
	
	public function canSave(){}
	
	
	private function getChildAssets(){
		// If we're looking at a container we need to work out the overall permissions, so return all the assets of this object if its a child.
		// Currently the only container is a book, but this may change in the future.
		if($this->m_resourceType == permissions::BOOK){
			$charts = chart::getBookCharts($this->m_assetID, 500);
			$this->m_childAssets['charts'] = $charts;
			
			foreach($this->m_childAssets['charts'] as &$chartAsset){
				$chart_pd = chart::isPublicDomain($this->m_userObj, $chartAsset['chID']);
				
				$chartAsset['publicDomain'] = $chart_pd;
				
				
				if($chart_pd == false) {
					$this->m_hasPrivateImages = true;
					array_push($this->m_restrictedAssets['charts'], $chartAsset);
				}
			}
			
		} else {
			return;
		}
	}
	
	public function get(){
		// Get all the details we have currently for this asset.
		return array("assetID" => $this->m_assetID, "childAssets" => $this->m_childAssets, "restrictedAssets" => $this->m_restrictedAssets, "hasPrivateImages" => $this->m_hasPrivateImages);
	}

}

?>